package com.dxd.onlineshop.controller;

import com.dxd.onlineshop.pojo.*;
import com.dxd.onlineshop.service.CategoriesService;
import com.dxd.onlineshop.service.UserService;
import com.dxd.onlineshop.utils.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/get")
public class QueryController {

    private final UserService userService;
    private final CategoriesService categoriesService;

    @Autowired
    public QueryController(UserService userService, CategoriesService categoriesService) {
        this.userService = userService;
        this.categoriesService = categoriesService;
    }

    //获取单个用户信息(客户端)
    @GetMapping("/userInfo")
    public Result<User> getUserInfo(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        try {
            Map<String,Object> claims = JwtUtil.parseToken(token);
            Integer userId = (Integer) claims.get("userId");
            User user = userService.getUserInfo(userId);
            return Result.success("User information retrieved successfully", user);
        } catch (Exception e) {
            return Result.error("Failed to retrieve user information: " + e.getMessage());
        }
    }

    //获取用户余额(客户端)
    @GetMapping("/userBalance")
    public Result<Integer> getUserBalance(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        try {
            Map<String,Object> claims = JwtUtil.parseToken(token);
            Integer userId = (Integer)claims.get("userId");
            int balance = userService.getUserBalanceById(userId);
            return Result.success("User balance retrieved successfully", balance);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("Failed to retrieve user balance: " + e.getMessage());
        }
    }

    //获取所有商品分类
    @GetMapping("/categories")
    public Result<List<Categories>> getCategories() {
        try {
            List<Categories> categoriesList = categoriesService.getCategories();
            System.out.println("从数据库获取到商品分类:" + categoriesList);
            return Result.success("Categories retrieved successfully", categoriesList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("Failed to retrieve categories: " + e.getMessage());
        }
    }

    //获取所有商品
    @GetMapping("/goods")
    public Result<List<Product>> getProducts() {
        try {
            List<Product> productList = categoriesService.getProducts();
            System.out.println("从数据库获取到所有商品:" + productList);
            return Result.success("Products retrieved successfully", productList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("Failed to retrieve products: " + e.getMessage());
        }
    }

    // 获取用户购物车信息
    @GetMapping("/cart")
    public Result<List<Cart>> getUserCartInfo( HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        try {
            Map<String, Object> claims = JwtUtil.parseToken(token);
            Integer userId = (Integer) claims.get("userId");
            List<Cart> cartList = userService.getUserCartInfo(userId);
            System.out.println("从数据库获取到用户购物车信息:" + cartList);
            return Result.success("User cart information retrieved successfully", cartList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("Failed to retrieve user cart information: " + e.getMessage());
        }
    }


}